package com.example.wishstar.view;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;
import androidx.room.Update;

import com.example.wishstar.R;
import com.example.wishstar.model.CommonDate;
import com.example.wishstar.model.UpdateHabit;
import com.example.wishstar.model.UpdateWish;
import com.example.wishstar.model.Wish;
import com.example.wishstar.utils.ScreenUtil;

import java.time.LocalDate;

public class UpdateWishDialogFragment extends DialogFragment {
    private String TAG = "UpdateWishDialogFragment";
    private ImageView updateWishImage;
    private Bundle bundle = null;
    private TextView updateWishTextTitle, updateImageBackground;
    private EditText updateWishEditTextDetails;
    private Button buttonCheck, buttonDelay;
    private ImageView imageClose;

    private Wish wish;
    private Context mcontext;
    private UpdateWishCallBack updateWishCallBack;

    public interface UpdateWishCallBack{
        void updateWish(UpdateWish wish);
        void delayWish(int wishId, CommonDate date);
    }

    public void setUpdateWishCallBack(UpdateWishCallBack updateWishCallBack) {
        this.updateWishCallBack = updateWishCallBack;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialogfragment_update_wish, container, false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        updateWishImage = view.findViewById(R.id.update_wish_image);
        updateWishTextTitle = view.findViewById(R.id.update_wish_title_text);
        updateWishEditTextDetails = view.findViewById(R.id.update_wish_edittext);
        buttonCheck = view.findViewById(R.id.update_wish_button_check);
        buttonDelay = view.findViewById(R.id.update_habit_button_delay);
        updateImageBackground = view.findViewById(R.id.update_wish_image_bg);
        imageClose = view.findViewById(R.id.update_wish_image_close);

        bundle = getArguments();
        mcontext = getContext();

        if (bundle != null) {
            wish = new Wish();
            //***
            wish.wishId = bundle.getInt("wishId");
            wish.wishTitle = bundle.getString("wishTitle");
            wish.wishIcon = bundle.getInt("wishIcon");

            Log.e(TAG, wish.toString());
            GradientDrawable gradientDrawable = (GradientDrawable) updateImageBackground.getBackground();
            gradientDrawable.setColor(mcontext.getColor(R.color.white));
            updateWishTextTitle.setText(wish.wishTitle);
            switch (wish.wishIcon) {
                case 1:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_agenda));
                    break;
                case 2:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_agriculture));
                    break;
                case 3:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_book));
                    break;
                case 4:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_cat));
                    break;
                case 5:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_economy));
                    break;
                case 6:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_haircut));
                    break;
                case 7:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_lipstick));
                    break;
                case 8:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_listen));
                    break;
                case 9:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_mask));
                    break;
                case 10:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_pet));
                    break;
                case 11:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_run));
                    break;
                case 12:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_shopping));
                    break;
                case 13:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_teeth));
                    break;
                case 14:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_sun));
                    break;
                case 15:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_swimming));
                    break;
                case 16:
                    updateWishImage.setImageDrawable(ContextCompat.getDrawable(mcontext, R.drawable.wish_item_icon_star));
                    break;
            }
        }

        buttonCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newWishDate = updateWishEditTextDetails.getText().toString();
                UpdateWish updateWish = new UpdateWish(wish.wishId, newWishDate);
                updateWishCallBack.updateWish(updateWish);
                dismiss();
            }
        });

        buttonDelay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog(v);
            }
        });

        imageClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        return view;
    }

    private void showDatePickerDialog(View view) {
        Calendar calendar=Calendar.getInstance();
        int themeResId = 3;
        new DatePickerDialog(mcontext, themeResId,  new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                CommonDate commonDate = new CommonDate(String.valueOf(year), String.valueOf(month + 1), String.valueOf(dayOfMonth));
                updateWishCallBack.delayWish(wish.wishId, commonDate);
                dismiss();
            }
        }
                ,calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    /**
     * 修改布局的大小
     */
    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
        resizeDialogFragment();
    }

    private void resizeDialogFragment() {
        Dialog dialog = getDialog();
        if (null != dialog) {
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
            lp.height = (ScreenUtil.getScreenHeight(getContext()) / 2);//获取屏幕的宽度，定义自己的宽度
            lp.width = (9 * ScreenUtil.getScreenWidth(getContext()) / 10);
            if (window != null) {
                window.setLayout(lp.width, lp.height);
            }
        }
    }

}
